﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CardInChina.Bussiness.Frontend.Interface;
using CardInChina.Data.Entity.Entity;

namespace CardInChina.Web.Mvc.Controllers
{
    public class PurchaseController : CICBasicController
    {

        private CardBusinessService cardbs = new CardBusinessService();
        //
        // GET: /Purchase/

        //commodity is key of class1
        //type is key of class2
        [SessionAuthorize]
        public ActionResult Index(string commodity, string value, string currency)
        {
            Card card = cardbs.GetCardByCardKey(value);
            if (card == null) throw new Exception("card not found!");
            card = card.Load<ICollection<ChargeOption>>();
            int currency_type = 8;
            string currencyName = "人民币";
            if (!string.IsNullOrEmpty(currency) && int.TryParse(currency, out currency_type))
            {
                ExchangeBusinessService exbs = new ExchangeBusinessService();
                card.Price = (float)Math.Round(exbs.GetCommomPriceOfCardByExchange(card, currency_type), 2);
                card.VIPPrice = (float)Math.Round(exbs.GetVIPPriceOfCardByExchange(card, currency_type), 2);
                currencyName = exbs.GetExchangeEntityByExchangeID(currency_type).CurrencyFullName;
            }


            ViewData.Model = card;


            ViewBag.Class1Key = commodity;
            ViewBag.IsVIP = CustomerInfo.IsVIP;
            ViewBag.CurrencyName = currencyName;
            ViewBag.Email = CustomerInfo.Email;

            return View();
        }

        public ActionResult Express(string commodity, string type)
        {
            return View();
        }

        [HttpPost]
        [SessionAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult Deal(FormCollection form)
        {
            // if create order success
            {
                // create order
                // TODO

                // set session
                CurrentUser.LastOrders = new int[] { 1 };

                return Redirect("/checkout");
            }
            // else
            {
                throw new ApplicationException("生成订单失败！");
            }
        }
    }
}
